﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Viewer.BusinessObject.BusinessEntities;
using Ext.Net;

namespace Viewer.NhanSu.UserControl
{
    public partial class UCKiLuat : System.Web.UI.UserControl
    {
        NSKiLuat kiLuat = new NSKiLuat();
        private List<NSKiLuat> lst;
        NSNhanVien nhanvien = new NSNhanVien();
        CommonCode common = new CommonCode();
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// Reset control
        /// </summary>
        private void ResetControl()
        {
            cbbCapKiLuat.SelectedItem.Value = string.Empty;
            txtKiLuat.Text = string.Empty;
            txtNoiDung.Text = string.Empty;
            txtGhiChu.Text = string.Empty;
            dtNgayHieuLuc.Text = string.Empty;
            UCQuyetDinh.ResetControl(common.KiLuat);
        }
        /// <summary>
        /// Show Dialog
        /// </summary>
        /// <param name="type"></param>
        /// <param name="hdId"></param>
        /// <param name="nhanvienID"></param>
        /// <param name="typeParent"></param>
        [DirectMethod]
        public void ShowDialog(string type, string hdId, string nhanvienID, string typeParent)
        {
            storeCapKiLuat.DataSource = common.LoadDataCommonByCodeKey(common.CapKiLuat);
            storeCapKiLuat.DataBind();

            hdNhanVienId.Text = nhanvienID;
            hdKiLuatId.Text = hdId;
            if (type.Equals("AddNew"))
            {
                wdKiLuat.Title = "Tạo mới kỉ luật";
                ResetControl();
                hdType.Text = type;
                btnSaveKiLuat.Hidden = false;
                hdParentType.Text = typeParent;
            }
            else if (type.Equals("Edit"))
            {
                wdKiLuat.Title = "Sửa đổi kỉ luật";
                hdType.Text = type;
                btnSaveKiLuat.Hidden = false;
                LoadDataNBL(Convert.ToInt16(hdId));
            }
            else if (type.Equals("View"))
            {
                wdKiLuat.Title = "Xem chi tiết kỉ luật";
                hdType.Text = type;
                btnSaveKiLuat.Hidden = true;
                LoadDataNBL(Convert.ToInt16(hdId));
            }
            wdKiLuat.Show();
        }
        /// <summary>
        /// Load Data theo id
        /// </summary>
        /// <param name="id"></param>
        public void LoadDataNBL(int id)
        {
            lst = (List<NSKiLuat>)Session["KL"];
            NSKiLuat item = lst.Where(a => a.KiLuatId.Equals(id)).SingleOrDefault();
            dtNgayHieuLuc.Text = item.NgayHieuLuc.HasValue && !item.NgayHieuLuc.Value.Year.Equals(1) ? item.NgayHieuLuc.Value.ToString("dd/MM/yyyy") : string.Empty;
            txtNoiDung.Text = item.HinhThucKiLuat;
            txtKiLuat.Text = item.TenKiLuat;
            txtGhiChu.Text = item.GhiChu;
            cbbCapKiLuat.SelectedItem.Value = item.CapKiLuatId.HasValue && item.CapKiLuatId.Value > 0 ? item.CapKiLuatId.ToString() : string.Empty;
            int qdID = item.QuyetDinhID.HasValue ? item.QuyetDinhID.Value : 0;
            hdQDID.Value = qdID.ToString();
            if (qdID > 0)
            {
                UCQuyetDinh.Command(common.CommandLoadByID, qdID);
            }
        }
        /// <summary>
        /// ButtonSave
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSaveKL_CLick(object sender, DirectEventArgs e)
        {
            int nhanVienId = !string.IsNullOrEmpty(hdNhanVienId.Text) ? Convert.ToInt16(hdNhanVienId.Text) : 0;
            int capKiLuatId = !string.IsNullOrEmpty(cbbCapKiLuat.SelectedItem.Value) ? Convert.ToInt16(cbbCapKiLuat.SelectedItem.Value) : 0;
            DateTime ngayHieuLuc = !dtNgayHieuLuc.Text.Equals("01/01/0001") ? Convert.ToDateTime(dtNgayHieuLuc.Text) : DateTime.MinValue;
            if (hdType.Text.Equals("AddNew"))
            {
                int qdID = UCQuyetDinh.TaoMoiQuyetDinh();
                NSKiLuat item = kiLuat.GetKiLuat(nhanVienId, txtKiLuat.Text, txtNoiDung.Text, capKiLuatId, txtGhiChu.Text, qdID, ngayHieuLuc);
                ((UCNhanVien)this.Parent).GetKiLuat(item);
            }
            else if (hdType.Text.Equals("Edit"))
            {
                int qdID = 0;
                //Edit quyết định
                if (Convert.ToInt16(hdQDID.Value) > 0)
                {
                    UCQuyetDinh.Command(common.CommandSua, Convert.ToInt16(hdQDID.Value));
                }
                else
                {
                    //Nếu không có record nào thì sẽ tạo mới
                     qdID = UCQuyetDinh.TaoMoiQuyetDinh();
                }
                kiLuat.EditKL(Convert.ToInt16(hdKiLuatId.Text), txtKiLuat.Text, capKiLuatId, txtNoiDung.Text, txtGhiChu.Text, qdID, ngayHieuLuc);
                ((UCNhanVien)this.Parent).ReloadGrid("KL");
            }

            wdKiLuat.Hide();
        }
    }
}